import { createStore, storeKey , Store , useStore as useVuexStore ,mapActions } from "vuex";

import { IRootState } from './type'
import type { IStoreType } from './type'

import login from './login/login'
import system from './main/system/system'
import dashboard from "./main/analysis/dashboard";
import { getPageListData } from '@/service/main/system/system'

const store = createStore<IRootState>({
  state: () => {
    return {
      name:"happy",
      age:12,
      entireDepartment:[],
      entireRole:[],
      entireMenu:[],
    }
  },
  mutations: {
    changeEntireDepartment(state,list){
      state.entireDepartment = list
    },
    changeEntireRole(state,list){
      state.entireRole = list
    },
    changeEntireMenu(state,list){
      state.entireMenu = list
    }
  },
  getters:{},
  actions: {
    async getInitialDataAction({commit}){
      //请求角色和部门数据
      const departmentRuselt= await getPageListData('/department/list',{offset:0,size:1000})
      const { list:departmentList } = departmentRuselt.data
      const roleResult = await getPageListData('/role/list',{offset:0,size:1000})
      const { list:roleList } = roleResult.data

      //请求菜单列表
      const menuResult = await getPageListData('/menu/list',{})
      const {list:menuList} = menuResult.data
      // console.log(departmentList,12121)
      //保存数据
      commit('changeEntireDepartment',departmentList)
      commit('changeEntireRole',roleList)
      commit('changeEntireMenu',menuList)
    }
  },
  modules: {
    login,
    system,
    dashboard,
  },
});

export function setupStore(){
  store.dispatch('login/loadLocalLogin')
  store.dispatch('getInitialDataAction')
}

export function useStore(): Store<IStoreType> {
 return useVuexStore()
}

export default store
